package com.efast.cafe.portal.service.company.impl;

import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.jdbc.JdbcDaoImpl;
import com.efast.cafe.framework.jdbc.persistence.Criteria;
import com.efast.cafe.framework.service.CafeServiceImpl;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.portal.dao.company.PortalCompanyOrgDao;
import com.efast.cafe.portal.entity.company.PortalCompanyOrg;
import com.efast.cafe.portal.service.company.IPortalCompanyOrgService;

@Service
@Transactional
public class PortalCompanyOrgServiceImpl extends CafeServiceImpl<PortalCompanyOrg> implements IPortalCompanyOrgService{

	private PortalCompanyOrgDao  	portalCompanyOrgDao;
	
	@Qualifier("jdbcDao")
	@Autowired
	private  JdbcDao  jdbcDao;
	
	
	
	
	@Autowired
	public void setPortalCompanyOrgDao(PortalCompanyOrgDao portalCompanyOrgDao) {
		this.portalCompanyOrgDao = portalCompanyOrgDao;
	}

	

	@Override
	public List<PortalCompanyOrg> queryPortalCompanyOrgList(Integer portal_company_org_id) {
		//Criteria criteria = Criteria.create(PortalCompanyOrg.class).where("company_code", new Object[]{SpringWebUtils.getRequestCompany()});
		/*return portalCpmpanyOrgDao.queryPortalCompanyOrgLIist();*/
		//return jdbcDao.queryList(criteria);
		return portalCompanyOrgDao.queryPortalCompanyOrgListById(0,SpringWebUtils.getRequestCompany(),portal_company_org_id);
	}
	
	@Override
	public List<PortalCompanyOrg> queryPortalCompanyOrgList() {
		//Criteria criteria = Criteria.create(PortalCompanyOrg.class).where("company_code", new Object[]{SpringWebUtils.getRequestCompany()});
		/*return portalCpmpanyOrgDao.queryPortalCompanyOrgLIist();*/
		//return jdbcDao.queryList(criteria);
		return portalCompanyOrgDao.queryPortalCompanyOrgList(SpringWebUtils.getRequestCompany());
	}


	@Override
	public PortalCompanyOrg queryCompanyOrgById(Long portalCompanyOrgId) {
		//return jdbcDao.get(PortalCompanyOrg.class, portalCompanyOrgId);
		return portalCompanyOrgDao.queryCompanyOrgById(SpringWebUtils.getRequestCompany(),portalCompanyOrgId);
	}


	@Override
	public void updateCompanyOrg(PortalCompanyOrg portalCompanyOrg) {
		/*List<PortalCompanyOrg> list = portalCpmpanyOrgDao.orgCodeIsExistWithId(portalCompanyOrg.getPortalCompanyOrgId(), portalCompanyOrg.getOrgCode());
		if (list.size()>0) {
			throw new ServiceException("msg.portal.company.org.exist");//org已经存在
		}*/
		int count = portalCompanyOrgDao.orgCodeIsExistWithId(portalCompanyOrg.getPortal_company_org_id(), portalCompanyOrg.getOrg_code(),SpringWebUtils.getRequestCompany());
		if (count != 0) {
			throw new ServiceException("msg.portal.company.org.exist");//org已经存在
		}
		jdbcDao.update(Criteria.create(PortalCompanyOrg.class)
				.set("org_name", portalCompanyOrg.getOrg_name())
				.set("org_type", portalCompanyOrg.getOrg_type())
				.set("org_desc", portalCompanyOrg.getOrg_desc())
				.set("org_code", portalCompanyOrg.getOrg_code())
				.where("portal_company_org_id", new Object[]{portalCompanyOrg.getPortal_company_org_id()})
				.and("company_code", new Object[]{SpringWebUtils.getRequestCompany()})
				);
	}


	@Override
	public Long insertCompanyOrg(PortalCompanyOrg portalCompanyOrg) {
		if(portalCompanyOrg.getParent_id()==null){
			portalCompanyOrg.setParent_id(0L);
		}
		Criteria criteria = Criteria.create(PortalCompanyOrg.class)
				.where("org_code",new Object[]{portalCompanyOrg.getOrg_code()})
				.and("company_code", new Object[]{SpringWebUtils.getRequestCompany()});
		int count = jdbcDao.queryCount(criteria);
		if (count != 0) {
			throw new ServiceException("msg.portal.company.org.exist");//org已经存在
		}
		
		Long id = jdbcDao.insert(Criteria.create(PortalCompanyOrg.class)
				.set("org_name", portalCompanyOrg.getOrg_name())
				.set("org_type", portalCompanyOrg.getOrg_type())
				.set("org_desc", portalCompanyOrg.getOrg_desc())
				.set("org_code", portalCompanyOrg.getOrg_code())
				.set("parent_id", portalCompanyOrg.getParent_id())
				.set("company_code", SpringWebUtils.getRequestCompany())
				.set("create_user", SpringWebUtils.getRequestUser())
				.set("create_time", new Date())
				);
		
		
		return id;
	}


	@Override
	public void deleteCompanyOrg(Long portalCompanyOrgId) {
		jdbcDao.delete(PortalCompanyOrg.class,portalCompanyOrgId);
	}

	@Override
	public PortalCompanyOrg getCurrentUserOrg() {
		return portalCompanyOrgDao.getCurrentUserOrg(SpringWebUtils.getRequestUser());
	}

	@Override
	public List<PortalCompanyOrg> getListFirstLevelOrgByCompanyUser() {
		return portalCompanyOrgDao.getListFirstLevelOrgByCompanyUser(SpringWebUtils.getRequestCompany());
	}



}
